<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-09
 * Time: 14:07
 */

namespace app\api\controller;


use app\common\model\Mall;
use app\common\model\User;
use app\model\CardsModel;
use app\model\UserModel;
use app\service\CardsService;
use app\service\CardThumbService;
use app\service\CardVisitedService;
use app\service\UserService;

class Cards extends Base
{
    /**
     * 查询名片
     */
    public function index()
    {
        $uniid = input('uniid', '');
        if ($uniid) {
            $user_id = UserService::S()->findUser(['uniid' => ['eq', $uniid]], 'id,mall_id,headimgurl,nickname,mobile');
            $where = [
                'user_id' => ['eq', $user_id['id']]
            ];
        } else {
            $where = [
                'user_id' => ['eq', $this->uid]
            ];
        }
        $data = CardsService::S()->findCard($where);
        if ($data) {
            $data['visited_list'] = CardVisitedService::S()->queryLimitList(['card_id' => ['eq', $data['id']]]);
            $data['visited_count'] = CardVisitedService::S()->queryCount(['card_id' => ['eq', $data['id']]]);
            $data['thumb_count'] = CardThumbService::S()->queryCount(['card_id' => ['eq', $data['id']]]);
            $where = [
                'card_id' => ['eq', $data['id']],
                'thumb_id' => ['eq', $this->uid],
            ];
            $data['is_thumb'] = CardThumbService::S()->findThumb($where) ? true : false;
        }else{
            $data['headimg'] = $user_id['headimgurl'];
            $data['name'] = $user_id['nickname'];
            $data['phone'] = $user_id['mobile'];
            $data['company'] = Mall::getOne(['id'=>['eq',$user_id['mall_id']]])['name'];
        }

        $this->ajax(200, '查询成功', $data);
    }

    /**
     * 添加名片
     */
    public function editCard()
    {
        $input = input('');
        if (!isset($input['headimg']) || empty($input['headimg']))
            $this->ajax(202, '头像不能为空');
        if (!isset($input['name']) || empty($input['name']))
            $this->ajax(202, '姓名不能为空');
        if (!isset($input['phone']) || empty($input['phone']))
            $this->ajax(202, '手机号不能为空');
        if (!isset($input['email']) || empty($input['email']))
            $this->ajax(202, '邮箱不能为空');
        //修改名片信息
        $save_data = [
            'headimg' => $input['headimg'],
            'name' => $input['name'],
            'professional' => $input['professional'],
            'phone' => $input['phone'],
            'wechat' => $input['wechat'],
            'company_wechat' => $input['company_wechat'],
            'tel' => $input['tel'],
            'email' => $input['email'],
            'company' => $input['company'],
            'sign' => $input['sign'],
            'pictures' => json_encode($input['pictures']),
            'create_time' => date('Y-m-d H:i:s'),
            'update_time' => date('Y-m-d H:i:s'),
            'type' => input('type', 1),
            'user_id' => $this->uid,
            'address' => $input['address'],
        ];
        $user_info = CardsService::S()->findCard(['a.user_id' => ['eq', $this->uid]], 'a.id');
        if ($user_info) {
            $res = CardsService::S()->editCard($save_data, ['id' => ['eq', $user_info['id']]]);
        } else {
            $res = CardsService::S()->addCard($save_data);
        }

        //修改用户信息
        $user_data = [
            'headimgurl' => $input['headimg'],
            'nickname' => $input['name'],
            'mobile' => $input['phone'],
        ];
        UserService::S()->saveUserInfo(['id' => ['eq', $this->uid]], $user_data);

        if ($res) {
            $this->ajax(200, '名片编辑成功', $res);
        } else {
            $this->ajax(202, '名片编辑失败');
        }

    }

    /**
     * 转账
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function transfer()
    {
        //接受数据
        $input = input('');

        //信息验证
        if (!$input['type']) {
            $this->ajax(202, '请选择您要转账的类型');
        }
        if ($input['uid'] && ($input['uid'] != $this->uid)) {//判断用户
            $user = UserModel::get($input['uid']);
            if($user){
                $user = $user->toArray();
            }else{
                $this->ajax(202, '用户不存在');
            }
        } else {
            $this->ajax(202, '用户不存在');
        }
        if ($input['money'] && ($input['money'] > 0)) {
            $has_money = 0;
            $input['money'] = round(abs($input['money']), 2);
            $user_info = UserModel::get($this->uid);
            if($user_info){
                $user_info = $user_info->toArray();
            }else{
                $this->ajax(202, '用户不存在');
            }
            switch ($input['type']) {
                case 1:
                    $has_money = $user_info['money'];
                    break;
                case 2:
                    $has_money = $user_info['score'];
                    break;
            }
            if ($has_money < $input['money']) {
                $this->ajax(202, '您填入的金额大于您拥有的金额');
            }
        } else {
            $this->ajax(202, '请填入正确的转账金额');
        }
        //转账者生成记录
        User::changeMoney($this->uid, $input['type'], -$input['money'], $user_info['nickname'] .'(id:'.$this->uid.')'. '转账给'  . $user['nickname'].'(id:'.$input['uid'].')');

        //收到转账的用户生成记录
        User::changeMoney($input['uid'], $input['type'], $input['money'],  $user['nickname'].'(id:'.$input['uid'].')' . '收到转账'  . $user_info['nickname'] . '(id:'.$this->uid.')');

        $this->ajax(200, '转账成功');

    }

    /**
     * 分享名片保存
     */
    public function sharePic()
    {
        $data = input('');
        if(isset($data['pic'])){
            $save_data = [
                'share_pic' => $data['pic']
            ];
            $res = CardsModel::update($save_data,['user_id'=>$this->uid]);
            if($res){
                $this->ajax(200,'修改成功');
            }else{
                $this->ajax(202,'修改失败');
            }
        }else{
            $this->ajax(202,'请填入分享图片的地址');
        }
    }
}